﻿using System.Collections.Generic;
using System.Reflection;
using System.Data;
using System.Text;

namespace Utility
{
    /// <summary>
    /// JSon数据操作类
    /// </summary>
    public class JSon
    {
        public JSon()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        #region 将 DataTable 结果集转换成 JSon 数据格式

        public static string DataTableToJSon(DataTable dt)
        {
            return DataTableToJSon(dt, string.Empty);
        }

        public static string DataTableToJSon(DataTable dt, string className)
        {
            StringBuilder Result = new StringBuilder(); //StringBuilder 取代 String 提高性能
            Result.Append("{\"");

            if (className.Equals(string.Empty))
            {
                className = dt.TableName.ToString();
            }

            Result.Append(className);
            Result.Append("\":[");

            for(int i = 0; i< dt.Rows.Count; i++)
            {
                Result.Append("{");

                for(int j = 0;j < dt.Columns.Count; j++)
                {
                    Result.Append("\"");
                    Result.Append(dt.Columns[j].ColumnName);
                    Result.Append("\":\"");
                    Result.Append(dt.Rows[i][j].ToString());
                    Result.Append("\",");
                }

                Result.Remove(Result.Length - 1, 1);
                Result.Append("},");
            }

            Result.Remove(Result.Length - 1,1);
            Result.Append("]");
            Result.Append("}");

            return Result.ToString();
        }

        #endregion

        #region 将 List<Object> 结果集转换成 JSon 数据格式

        /// <summary>
        /// 获取对象的全部属性和值,保存方式如：属性1：值1
        /// </summary>
        /// <param name="o"> 对象 </param>
        /// <returns> 字符串列表 </returns>
        public static List<string> GetObjectProperty(object o)
        {
            List<string> propertysList = new List<string>();            //字符串列表对象
            PropertyInfo[] propertys = o.GetType().GetProperties();     //反射获取对象所有属性有属性值

            foreach (PropertyInfo p in propertys)
            {
                string item = "\"" + p.Name.ToString() + "\":\"" + p.GetValue(0,null) + "\"";     
                propertysList.Add(item);
            }

            return propertysList;
        }

        /// <summary>
        /// 将一个对象的所有属性和属性值按json的格式要求输入为一个封装后的结果。
        /// </summary>
        /// <param name="o"></param>
        /// <returns> 返回值类似{"属性1":"属性1值","属性2":"属性2值","属性3":"属性3值"} </returns>
        public static string OneObjectToJSon(object o)
        {
            string Result = "{";

            List<string> ls_propertys = new List<string>();
            ls_propertys = GetObjectProperty(o);

            foreach (string str_Property in ls_propertys)
            {
                if (Result.Equals("{"))
                {
                    Result = Result + str_Property;
                }
                else
                {
                    Result = Result + "," + str_Property + "";
                }
            }

            Result = Result + "}";
            return Result.ToString();
        }

        public static string ListToJSon(List<object> objlist)
        {
            return ListToJSon(objlist, string.Empty);
        }

        /// <summary>
        /// 把对象列表转换成json串
        /// </summary>
        /// <param name="objlist"> 对象列表 </param>
        /// <param name="className"></param>
        /// <returns></returns>
        public static string ListToJSon(List<object> objlist, string className)
        {
            StringBuilder Result = new StringBuilder();            
            Result.Append("{");

            if (className.Equals(string.Empty)) //未给定类名称，自定义一个
            {
                object o = objlist[0];
                className = o.GetType().ToString();
            }

            Result.Append("\"" + className + "\":[");

            bool firstline = true;      //处理第一行前不加“，”号

            foreach (object oo in objlist)
            {
                if (!firstline)
                {
                    Result.Append("," + OneObjectToJSon(oo));
                }
                else
                {
                    Result.Append(OneObjectToJSon(oo));
                    firstline = false;
                }
            }

            Result.Append("]}");
            return Result.ToString();
        }       

        #endregion

    }
}
